/**
 * @author harae Feng 
 * @date 2019-09-23 17:40:11 
 * @Last Modified by: harae Feng
 * @Last Modified time: 2019-09-23 18:15:09
 * @Describe 酷我搜索-歌手搜索
 */

 const fetch = require('../../util/fetchUtil').fetchRequest
 const ResultAPI = require('../../routes/api/resultAPI')
 const CatchHandle = require('../../routes/api/apicatch')


 /**
  * 歌手搜索
  * @param {String} keyword 
  * @param {Int} pn 
  * @param {Int} rn 
  */
 async function searchArtist(keyword, pn=1, rn=20){
    let url = `http://www.kuwo.cn/api/www/search/searchArtistBykeyWord?key=${keyword}&pn=${pn}&rn=${rn}`
    let res = await fetch(url)
    let resJSON = await res.json()
    return resJSON
 }

 async function searchArtistAPI(ctx){
    try {
        const keyword = ctx.query.keyword
        const pn = ctx.query.pn
        const rn = ctx.query.rn

        if(!keyword){return new ResultAPI(10000)}

        let resJSON = await searchArtist(keyword, pn, rn)
        if(resJSON.code === 200){
            return new ResultAPI(0, {}, resJSON.data)
        }else{
            return new ResultAPI(10003)
        }       
    } catch (error) {
        return CatchHandle(__dirname, __filename, error)
    }
 }


 module.exports = {
    searchArtistAPI: searchArtistAPI
 }